package com.tsunami.auth.controller;

import com.tsunami.auth.service.auth.IResourceService;
import com.tsunami.auth.service.auth.IUserService;
import com.tsunami.authority.dto.auth.ResourceQueryDTO;
import com.tsunami.authority.dto.auth.ResourceSaveDTO;
import com.tsunami.authority.dto.auth.ResourceUpdateDTO;
import com.tsunami.authority.entity.auth.Resource;
import com.tsunami.base.R;
import com.tsunami.base.entity.SuperEntity;
import com.tsunami.log.annotation.SysLog;
import com.tsunami.user.annotation.LoginUser;
import com.tsunami.user.model.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Classname ResourceController
 * @Description TODO
 * @Date 2021-11-18 10:00
 * @Created by lx
 */
@RestController
@Api(tags = "resourceController", description = "权限管理")
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private IResourceService resourceService;

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "查询资源(分页)", notes = "查询资源(分页)")
    @GetMapping("/page")
    @SysLog("查询资源(分页)")
    public R page(Integer pageNum,
                  Integer pageSize,
                  String orderByColumn,
                  String isAsc,
                  Resource data) {
        return resourceService.selectPageList(data);
    }

    @GetMapping("get")
    @ApiOperation(notes = "查询资源",value = "查询资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "资源ID", dataType = "Long", paramType = "query", defaultValue = ""),
    })
    @SysLog("查询资源")
    public R get(Long id){
        return R.ok().only(resourceService.getById(id));
    }


    @ApiOperation(value = "新增资源", notes = "新增资源不为空的字段")
    @PostMapping("save")
    @SysLog("新增资源")
    public R save(@LoginUser SysUser user, @Validated ResourceSaveDTO data){
        Resource resource = new Resource();
//        resource.setTenantId();
        resource.setCode(data.getCode());
        resource.setName(data.getName());
        resource.setMenuId(data.getMenuId());
//        resource.setMethod(data.get);
//        resource.setUrl(data.);
        resource.setDescribe(data.getDescribe());
        resource.setCreateUser(user.getId());
        resourceService.save(resource);
        return R.ok();
    }

    @ApiOperation(value = "修改资源", notes = "修改资源")
    @PutMapping("/update")
    @SysLog("修改资源")
    public R update(@LoginUser SysUser user, @RequestBody @Validated(SuperEntity.Update.class) ResourceUpdateDTO data) {
        resourceService.updateResource(user,data);
        return R.ok();
    }

    @ApiOperation(value = "删除资源", notes = "根据id物理删除资源")
    @DeleteMapping("/delete")
    @SysLog("删除资源")
    public R delete(@RequestParam("ids[]") List<Long> ids) {
        resourceService.removeByIds(ids);
        return R.ok();
    }

    @ApiOperation(value = "查询用户可用的所有资源", notes = "查询用户可用的所有资源")
    @GetMapping("/visible")
    @SysLog("查询用户可用的所有资源")
    public R visible(@LoginUser SysUser user, ResourceQueryDTO resource) {
        if (resource == null) {
            resource = new ResourceQueryDTO();
        }

        if (resource.getUserId() == null) {
            resource.setUserId(user.getId());
        }
        List<Resource> list = resourceService.findVisibleResource(resource);
        return R.ok().data(list);
    }

    @ApiOperation(value = "查询所有资源", notes = "查询所有资源")
    @GetMapping("/list")
    @SysLog("查询所有资源")
    public R list() {
        List<Resource> list = resourceService.list();
        List<String> resourceList = list.stream().map((Resource r) -> {
            return r.getMethod() + r.getUrl();
        }).collect(Collectors.toList());
        return R.ok().data(resourceList);
    }

    @ApiOperation(value = "初始化资源角色关联数据", notes = "初始化资源角色关联数据")
    @GetMapping("/initResourceRolesMap")
    public R initResourceRolesMap(){
        Map<String,Object> resourceRolesMap = resourceService.initResourceRolesMap();
        return R.ok();
    }

    @ApiOperation(value = "查询菜单资源", notes = "查询菜单所有资源")
    @GetMapping("/byMenu")
    public R resourceByMenu(Long menuId){
        List<Resource> list = resourceService.findResourceByMenu(menuId);
        return R.ok().data("data",list);
    }

    /**
     * feign远程调用 根据账号获取用户
     * @param userId
     * @return
     */
    @RequestMapping(value="/findVisibleResource", method= RequestMethod.GET)
    public R findVisibleResource(Long userId){
        List<String> permissionValueList = resourceService.findVisibleResource(userId);
        return R.ok().data(permissionValueList);
    }


}
